﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Reflection;
using XinYuan.Office.Models;
namespace XinYuanOfficeWinForm.Admin
{
    public enum FormOpenState
    { Open, Close }
    public partial class FrmMain : Form
    {
        string func = "";
        FrmLogin frmLogin;
        public FrmMain(FrmLogin frmLogin)
        {
            InitializeComponent();
            this.frmLogin = frmLogin;
        }
        public FrmMain()
        {
            InitializeComponent();
        }
        RoleServiceReference.RoleServiceClient roleClient = new RoleServiceReference.RoleServiceClient();
        OperatorServiceReference.OperatorServiceClient operatorClient = new OperatorServiceReference.OperatorServiceClient();
        private void OpenWindow(string ChildTypeString)
        {
            Form myChild = null;
            if (!ContainMDIChild(ChildTypeString))
            {
                // Get current process assembly
                Assembly assembly = Assembly.GetExecutingAssembly();
                // Create data type using type string
                Type typForm = assembly.GetType(ChildTypeString);
                // Create object using type's "InvokeMember" method
                Object obj = typForm.InvokeMember(null, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance, null, null, null);
                // Show child form 
                if (obj != null)
                {
                    myChild = obj as Form;
                    myChild.MdiParent = this;
                    myChild.Show();
                    myChild.Focus();
                }
            }
        }


        private bool ContainMDIChild(string ChildTypeString)
        {
            Form myMDIChild = null;
            foreach (Form f in this.MdiChildren)
            {
                if (f.GetType().ToString() == ChildTypeString)
                {
                    // found it 
                    myMDIChild = f;
                    break;
                }
            }
            // Show the exist form
            if (myMDIChild != null)
            {
                myMDIChild.TopMost = true;
                myMDIChild.Show();
                myMDIChild.Focus();
                return true;
            }
            else
                return false;
        }
        private void tsbShowMenu_Click(object sender, EventArgs e)
        {
            this.splitter1.Visible = this.tvwMenu.Visible = !this.tvwMenu.Visible;
        }

        private void tsbExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认退出程序","系统提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Information)==DialogResult.OK)
            {
                Application.Exit();
            }
        }
        #region Method
        #region 退出
        /// <summary>
        /// 退出
        /// </summary>
        private void Exit()
        {
            if (MessageBox.Show("确认退出系统吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                Application.Exit();
            }
        }
        #endregion

        /// <summary>
        /// 加载树形菜单
        /// </summary>
        private void LoadTreeNode()
        {
            string rootNodeText = ConfigurationManager.AppSettings["CommpanyName"].ToString();
            TreeNode rootNode = new TreeNode(rootNodeText);
            rootNode.Tag = "-1";
            rootNode.Name = "rootNode";
            tvwMenu.ImageIndex = 0;
            tvwMenu.SelectedImageIndex = 0;
            tvwMenu.Nodes.Add(rootNode);
            rootNode.Expand();

            #region 个人中心
            TreeNode node1 = new TreeNode();
            node1.Name = "ndPseronCenter";
            node1.Text = "个人中心";
            node1.ImageIndex = 0;
            node1.SelectedImageIndex = 0;
            tvwMenu.Nodes["rootNode"].Nodes.Add(node1);
            TreeNode node1_1 = new TreeNode();
            node1_1.Name = "ndEmail";
            node1_1.Text = "我的邮件";
            node1_1.ImageIndex = 0;
            node1_1.SelectedImageIndex = 0;
            node1.Nodes.Add(node1_1);
            TreeNode node1_2 = new TreeNode();
            node1_2.Name = "ndShortMsg";
            node1_2.Text = "我的短信";
            node1_2.ImageIndex = 0;
            node1_2.SelectedImageIndex = 0;
            node1.Nodes.Add(node1_2);
            TreeNode node1_3 = new TreeNode();
            node1_3.Name = "ndPlan";
            node1_3.Text = "我的日程";
            node1_3.ImageIndex = 0;
            node1_3.SelectedImageIndex = 0;
            node1.Nodes.Add(node1_3);
            TreeNode node1_4 = new TreeNode();
            node1_4.Name = "ndBeDone";
            node1_4.Text = "我的待办";
            node1_4.ImageIndex = 0;
            node1_4.SelectedImageIndex = 0;
            node1.Nodes.Add(node1_4);
            node1.Expand();
            #endregion
        }
        #endregion

        private void FrmMain_Load(object sender, EventArgs e)
        {
            func = roleClient.GetFunctionByOperatorId(User.Id);
            LoadTreeNode();
            ShowMenu();
            TraverseMenu();
        }
        void TraverseMenu()
        {
            foreach (Control c in this.Controls)
            {
                if (c is MenuStrip)
                {
                    MenuStrip menu = c as MenuStrip;
                    for (int i = 0; i < menu.Items.Count; i++)
                    {
                        if (func.Contains(menu.Items[i].Name))
                            tvwMenu.Nodes["rootNode"].Nodes.Add(menu.Items[i].Name, menu.Items[i].Text.Substring(0, menu.Items[i].Text.IndexOf("(")));
                        ToolStripMenuItem tsi = menu.Items[i] as ToolStripMenuItem;
                        for (int j = 0; j < tsi.DropDownItems.Count; j++)
                        {
                            if (func.Contains(tsi.DropDownItems[j].Name))
                            {
                                int index = tsi.DropDownItems[j].Text.IndexOf("(");
                                if (index >= 0)
                                    tvwMenu.Nodes["rootNode"].Nodes[i+1].Nodes.Add(tsi.DropDownItems[j].Name, tsi.DropDownItems[j].Text.Substring(0, index));
                            }
                        }
                    }
                }
            }
        }
        public void ShowMenu()
        {
            foreach (Control c in this.Controls)
            {
                if (c is MenuStrip)
                {
                    MenuStrip menu = c as MenuStrip;//转换为菜单
                    for (int i = 0; i < menu.Items.Count; i++)
                    {
                        ToolStripMenuItem m = menu.Items[i] as ToolStripMenuItem;
                        m.Visible = false;
                        if (func.Contains(m.Name))
                        {
                            m.Visible = true;
                        }
                        for (int j = 0; j < m.DropDownItems.Count; j++)
                        {
                            m.DropDownItems[j].Visible = false;
                            if (func.Contains(m.DropDownItems[j].Name))
                            {
                                m.DropDownItems[j].Visible = true;
                            }
                        }
                    }
                }
            }
        }

        private void tvwMenu_DoubleClick(object sender, EventArgs e)
        {
            TreeNode node = tvwMenu.SelectedNode;
            if (node == null)
            {
                return;
            }
            switch (node.Text.Trim())
            {
                case "权限配置":
                    OpenWindow(typeof(Admin.Permission.FrmRights).ToString());
                    break;
                default:
                    break;
            }
        }
        private void tsmiRightSetting_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(Admin.Permission.FrmRights).ToString());
        }
        #region Method
        bool TraverseForm(string str, ref Form f)
        {
            Assembly a = Assembly.LoadFile(Application.ExecutablePath);
            Type[] types = a.GetTypes();
            foreach (Type t in types)
            {

                Console.WriteLine(t.Name);
                if (t.BaseType.Name == "Form" && t.Name == str)
                {
                    f = (Form)Activator.CreateInstance(t, true);
                    return true;
                }
            }
            return false;
        }
        void TraverseMenu(Control control)
        {
            foreach (Control c in control.Controls)
            {
                if (c is MenuStrip)
                {
                    MenuStrip menu = c as MenuStrip;
                    foreach (ToolStripMenuItem item in menu.Items)
                    {
                        Code.FormMenu formMenu = new Code.FormMenu();
                        formMenu.MenuID = item.Name;
                    }
                }
            }
        }
        #endregion

        private void tsmiBaseInfoSet_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(Admin.BaseConfig.FrmBaseInfos).ToString());
        }

        private void tsbExit_ButtonClick(object sender, EventArgs e)
        {
            this.Exit();
        }

        private void tsmiCancel_Click(object sender, EventArgs e)
        {
            
        }

        private void tsbtnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            frmLogin.Show();
        }
    }
}
